package com.scpii.universal.hibernate.dao.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.UserInfoDto;
import com.scpii.universal.hibernate.dao.CatelogDao;
import com.scpii.universal.hibernate.entity.CatelogTab;

/**
 * universalWeb
 * @author liuhj
 * 2012-7-5下午05:16:31
 */
@Repository
public class CatelogDaoImpl extends BaseDaoImpl<CatelogTab, Integer> implements CatelogDao {

	@Override
	public void delete(Integer id) {
		String hql = "update CatelogTab c set c.intDeleteSign=?,c.datUpdateDatetime=? " +
				"where c.intCateId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(1,
						id).setParameter(1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
							.format(new Date())).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> getNextLevelChildren(Integer id) {
		String hql = "from CatelogTab c where c.intParentCateId=? and c.intDeleteSign=? " +
				"order by c.intCateId asc";
		return (List<CatelogTab>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0, id)
				.setParameter(1, GenericConstants.DELETE_SIGN_EXIST).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> getAll(UserInfoDto user) {
		String[] properties = { "intCateId", "varCateName", "intParentCateId",
				"varCateImgUrl" ,"intHasSubCate", "varCateDescrip", "intType", "intPrivilege"};
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intAppId=? and entity.intEnterpId=?";
		return (List<CatelogTab>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1,
						user.getAppId()).setParameter(2, user.getIntEnterpId()).list();
	}

	@Override
	public CatelogTab getCatelogInfoById(int id, String[] properties) {
		String hql = generateHqlByProperties(properties) + "where entity.intCateId=?";
		return (CatelogTab) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0, id)
				.uniqueResult();
	}

	@Override
	public void updateParentHasSubCate(int id,int hasSubCate) {
		String hql = "update CatelogTab c set c.intHasSubCate=?,c.datUpdateDatetime=? " +
				"where c.intCateId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,hasSubCate).setParameter(
						1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
						.setParameter(2, id).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> getSuperiorCatelogs(int id) {
		StringBuffer sql = new StringBuffer();
		sql.append("select {cate.*} from CATELOG_TAB cate");
		sql.append(" where cate.INT_DELETE_SIGN=?");
		sql.append(" start with cate.INT_CATE_ID=?");
		sql.append(" connect by prior cate.INT_PARENT_CATE_ID=cate.INT_CATE_ID");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql.toString()).addEntity("cate",
						CatelogTab.class).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1, id).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> getSubordinateCatelogs(int id) {
		StringBuffer sql = new StringBuffer();
		sql.append("select {cate.*} from CATELOG_TAB cate");
		sql.append(" where cate.INT_DELETE_SIGN=?");
		sql.append(" start with cate.INT_CATE_ID=?");
		sql.append(" connect by prior cate.INT_CATE_ID=cate.INT_PARENT_CATE_ID");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql.toString()).addEntity("cate",
						CatelogTab.class).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1, id).list();
	}

	@Override
	public void delete(String ids) {
		String hql = "update CatelogTab c set c.intDeleteSign=?,c.datUpdateDatetime=? "
				+ "where c.intCateId in" + ids;
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(
						1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> getNextLevelChildren(Integer id, String[] properties) {
		String hql = generateHqlByProperties(properties) + 
			"where entity.intDeleteSign=? and entity.intParentCateId=?";
		return (List<CatelogTab>) getHibernateTemplate().getSessionFactory()
			.getCurrentSession().createQuery(hql).setParameter(0, GenericConstants.DELETE_SIGN_EXIST)
			.setParameter(1, id).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> getNextLevelChildren(Integer appId, Integer id,
			String[] properties) {
		String hql = generateHqlByProperties(properties)
				+ "where entity.intDeleteSign=? and entity.intParentCateId=? and entity.intAppId=?";
		return (List<CatelogTab>) getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createQuery(hql).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1, id)
				.setParameter(2, appId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatelogTab> findBySql(final String sql, final Map<String, Object> map) {
		List<CatelogTab> lstResult = null;
		lstResult = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql).addEntity("cate", CatelogTab.class);
				if (map != null) {
					Set<String> keySet = map.keySet();
					for (String string : keySet) {
						Object obj = map.get(string);
						if (obj instanceof Collection<?>) {
							query.setParameterList(string, (Collection<?>) obj);
						} else if (obj instanceof Object[]) {
							query.setParameterList(string, (Object[]) obj);
						} else {
							query.setParameter(string, obj);
						}
					}
				}
				return query.list();
			}
		});
		return lstResult;
	}
}
